<?php
/**
 * User: aka
 * Time: 23:01
 */

namespace app\tenno\controller;

use think\Db;
use think\facade\Session;
use think\Request;

class ClanActivity
{
    private $userId;

    public function __construct()
    {
        $this->userId = Session::get('user_info.id');
    }

    public function __destruct()
    {
        unset(
            $this->userId
        );
    }

    public function index()
    {
        $clanList = \app\tenno\model\ClanActivity::deleteState(0)
            ->paginate(10)
            ->each(
                function($item)
                {
                    $item->uid = \app\tenno\model\Users::userInfo($item->uid)
                        ->find();
                }
            );
        return view('index', ['clan_list' => $clanList]);
    }

    public function sendClanActivity()
    {
        return view('send_clan_activity');
    }

    public function activityContent(Request $request)
    {
        $activityId = base64_decode($request->param('id'));
        $activityContent = \app\tenno\model\ClanActivity::content(0, $activityId)
            ->find();
        $activityContent['uid'] = \app\tenno\model\Users::userInfo($activityContent['uid'])
            ->find();
        $activityData = \app\tenno\model\ClanActivity::get($activityId);
        $activityData->page_views	=  Db::raw('page_views+1');
        $activityData->save();
        return view('activity_content', ['activity_data' => $activityContent]);
    }

    public function myActivity()
    {
        $clanList = \app\tenno\model\ClanActivity::deleteState(0)
            ->uid($this->userId)
            ->paginate(10)
            ->each(
                function($item)
                {
                    $item->uid = \app\tenno\model\Users::userInfo($item->uid)
                        ->find();
                }
            );
        return view('my_activity', ['clan_list' => $clanList]);
    }
}